package com.itheima;

import com.itheima.controller.CentroController;
import com.itheima.controller.UserController;
import com.itheima.domain.Option;
import com.itheima.domain.User;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

//客户端程序启动入口
public class ClientApplication {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        //login();
        while (true) {
            //1. 展示一级菜单列表
            System.out.println("--------欢迎登录黑马信息管理系统-------");
            System.out.println("1. 登录账号");
            System.out.println("2. 注册账号");
            System.out.println("3. 退出");

            //2. 用户录入选项,匹配对应功能
            String option = sc.next();

            //3. 基于switch来进行菜单功能路由
            //switch小括号里本质上就是要一个值.值的类型支持: byte, short, int, char, jdk1.5开始支持枚举,jdk1.7开始支持String
            switch (option) {
                case "1":
                    CentroController login = login();
                    login.start();
                    break;       //就近原则
                case "2":
                    User register = register();
                    if (register != null){
                        System.out.println("正在为您登录...");
                        (new CentroController(register)).start();
                    }
                    break;
                case "3":
                    System.out.println("欢迎您的使用,再见!");
                    System.exit(0);  // 退出当前虚拟机. 0 : 表示让虚拟安心的走.
                default:
                    System.out.println("输入有误!");
                    break;
            }
        }
    }

    private static CentroController login() {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i <= 3; i++) {
            System.out.println("请输入用户名:");
            String name = sc.nextLine();
            System.out.println("请输入用户密码:");
            String pwd = sc.nextLine();
            //调用方法查询用户
            User user = getUser(name);
            if (user != null && user.getPassworld().equals(pwd)){
                return new CentroController(user);
            }else{
                System.out.println("用户名或密码错误,剩余次数:" + (3-i));
            }
        }
        System.exit(0);
        return null;
    }

    //注册功能
    private static User register() throws IllegalAccessException, InstantiationException {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = sc.nextLine();
        System.out.println("请输入用户密码:");
        String pwd = sc.nextLine();
        //通过反射调用usercontroller的add方法
        Class<UserController> userControllerClass = UserController.class;
        UserController userController = userControllerClass.newInstance();

        User user = userController.addUser(name, pwd);
        if (user != null){
            System.out.println("注册成功!权限提升请联系管理员");
            return user;
        }else {
           // System.out.println("用户名重复,添加失败");
        }
        return null;
    }

    private static User getUser(String nume) {
        try {
            Socket socket = new Socket("127.0.0.1", 10001);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(Option.LOGIN + "&" + nume);
            bw.newLine();
            bw.flush();

            //2. 接收服务器响应回来的结果
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            User user = (User) ois.readObject();
            ois.close();
            socket.close();

            //3. 判断是否添加成功
            if (user != null)
                return user;
            else
                return null;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
